
// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;
        return obj;
    }

    void Delete(void* obj)
    {
        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存剩余的字节数
    void* _freeList = nullptr; // 管理还回来的内存对象的自由链表
};


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;

// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;
         
        if(_freeList)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;

        return obj;
    }

    void Delete(void* obj)
    {
        assert(obj);

        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存剩余的字节数
    void* _freeList = nullptr; // 管理还回来的内存对象的自由链表
};



#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;

// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;

        return obj;
    }

    void Delete(void* obj)
    {
        assert(obj);

        *((void**)obj) = _freeList;
        _freeList = obj;
    }


private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存剩余的字节数
    void* _freeList = nullptr; // 管理切好的内存对象的自由链表
};


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;


// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList != nullptr)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) : sizeof(void*) ? sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;

        return obj;
    }

    void Delete(void* obj)
    {
        assert(obj);

        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存剩余的字节数
    void* _freeList = nullptr; // 管理切好的内存对象的自由链表
};


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;



// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList != nullptr)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;

        return obj;
    }

    void Delete(T* obj)
    {
        assert(obj);

        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;       // 大块内存剩余的字节数
    void* _freeList = nullptr;   // 管理还回来的内存对象的自由链表
};


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;


// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList != nullptr)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;

        return obj;
    }


    void Delete(T* obj)
    {
        assert(obj);

        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存所占的字节数
    void* _freeList = nullptr; // 管理还回来的内存对象的自由链表
};


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>

using std::cout;
using std::endl;


// 定长内存池 - 申请和释放固定大小的内存对象
template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList != nullptr)
        {
            obj = (T*)_freeList;
            _freeList = *((void**)_freeList);
        }
        else 
        {
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(nullptr == _memory)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        new(obj)T;
        return obj;
    }

    void Delete(T* obj)
    {
        assert(obj);

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr;
    size_t _leftBytes = 0;
    void* _freeList = nullptr;
};



// 定长内存池
template<class T> 
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        // 优先从自由链表中获取内存对象
        if(_freeList)
        {
            void* next = *((void**)_freeList);
            obj = (T*)_freeList;
            _freeList = next;
        }
        else
        {
            // 自由链表为空时再从大块内存中申请
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;
                _memory = (char*)malloc(_leftBytes);

                if(_memory = nullptr)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        // 定位new，调用T的构造函数显示初始化
        new(obj)T;

        return obj;
    }

    void Delete(void* obj)
    {
        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块空间的起始地址
    size_t _leftBytes = 0;   // 大块内存剩余的字节数
    void* _freeList = nullptr; // 管理切回来的大块内存的起始地址
};



// 定长内存池

template<class T>
class ObjectPool
{
public:
    T* New()
    {
        T* obj = nullptr;

        if(_freeList)
        {
            void* next = *((void**)_freeList);
            obj = (T*)_freeList;
            _freeList = next;
        }
        else
        {
            // 优先从自由链表中申请内存对象，自由链表中没有的时候再从内存空间中申请
            size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);

            if(_leftBytes < objSize)
            {
                _leftBytes = 128 * 1024;

                _memory = (char*)malloc(_leftBytes);

                if(_memory == nullptr)
                {
                    throw std::bad_alloc();
                }
            }

            obj = (T*)_memory;
            _memory += objSize;
            _leftBytes -= objSize;
        }

        // 使用定位new来初始化
        new(obj)T;

        return obj;
    }

    void Delete(void* obj)
    {
        assert(obj);

        obj->~T();

        *((void**)obj) = _freeList;
        _freeList = obj;
    }

private:
    char* _memory = nullptr; // 指向大块内存的起始地址
    size_t _leftBytes = 0;   // 大块内存的剩余空间的大小
    void* _freeList = nullptr; // 管理切好的内存对象的自由链表
};


